#include "timesUs.h"
unsigned short arr_time[6] = {0};

// 使用递归。 输入当前年(2023)，返回历年闰年的次数
int year_run(int year)
{
    int count = 0;
    if (year < 1972)
        return 0;

    if (year == 1972)
        return 1;

    if ((year - 1970) % 4 == 0)
        count++;

    return count + year_run(year - 1);
}

// 输入年，嵌套计算多少个闰年
int year_determine(int year)
{
    static int count = 0;
    if (year == 1972)
    {
        return 1;
    }
    if (year < 1972)
    {
        return 0;
    }
    if (year < 1970)
    {
        return 0;
    }
    return year_run(year - 4);
}

// 输入一个值，当下次输入值改变时，返回程序在该值改变的时间里执行的次数。用于计算fps
int FPS_exe(int time)
{

    static int time_static = 0, count = 0, count_temp = 0;

    count_temp++;
    if (time_static != time)
    {

        count = count_temp;
        count_temp = 0;
        time_static = time;
    }
    return count;
}

// 输入存储时分秒的数组。时分秒按顺序放进数组中从第 n - 1 个元素开始存，如果元素越界，结束并 return -1，且时间值不会存储进数组
int Hour_Min_Sec()
{
    int hour, min, sec;
    long now = time(NULL);

    hour = (now / (60 * 60) + 8) % 24;
    min = now / 60 % 60;
    sec = now % 60;

    arr_time[3] = hour;
    arr_time[4] = min;
    arr_time[5] = sec;

    // printf("%.2d:%.2d:%.2d %.6d", hour, min, sec);
    //  printf("%.2d:%.2d:%.2d %.6d", hour, min, sec, count);
    return 1;
}

// 计算或者位操作将时分秒组合后单次返回出去再解码(hour << 16 | min << 8 | sec)<<<单纯为了一个参数输出
int time_return(long now)
{
    int hour, min, sec, temp;

    hour = (now / (60 * 60) + 8) % 24;
    min = now / 60 % 60;
    sec = now % 60;

    temp = hour << 16 | min << 8 | sec;
    return temp;
}

// 输入time(NULL), 返回0 - 6。   0代表周一，6代表周日
int printf_week(unsigned long now)
{
    if (now <= 604800 + 345600 - 28800)
    {
        now = (now - 345600 + 28800) / (60 * 60 * 24);
        // printf("星期%s", geek[now]);或者直接打印当前星期
        return now;
    }

    return printf_week(now - 604800);
}

// 输入存储年月日的数组。返回闰年的个数，年月日的数字按顺序放进数组中从第n - 1个元素开始存,如果元素越界，结束并且return -1;
int Year_moth_day()
{
    int gmon[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int year = 1970, run_day = 0, year_up = 365, year_count = 3;
    long now = time(NULL);
    int day = (now + 28800) / (24 * 60 * 60);
    int i = 0;

    for (i = 0; day >= year_up; i++)
    {
        day -= year_up;
        if (year_count % 4 == 0)
        {
            year_up = 366;
            run_day++;
        }
        else
        {
            year_up = 365;
        }
        year_count++;
        year++;
    }

    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
    {
        run_day--;
        gmon[1] = 29;
    }
    else
    {
        gmon[1] = 28;
    }

    day++;
    for (i = 0; day > gmon[i]; i++) // 循环出月
    {
        day -= gmon[i];
    }
    // if( n + 3 >= sizeof(arr) / sizeof(arr[0]))
    // {
    //     printf("error");
    //     return -1;
    // }
    arr_time[0] = year;
    arr_time[1] = i + 1;
    arr_time[2] = day;

    return run_day;
}
